<?php
/**
 * +------------------------------------------------------------+
 * @category IndexAction
 * +------------------------------------------------------------+
 * 使用及关注药物
 * +------------------------------------------------------------+
 *
 * @author funwee@qq.com
 * @copyright http://www.suncco.com 2013
 * @version 1.0
 *
 * Modified at : 2013-10-22 上午10:51:44
 *
 */
class IndexAction extends Action{

	function index(){
		$categoryList = D('GroupCategory')->select();
		$user = D('User')->find($this->mid);
		
		if($this->isPost()){
//			dump($_POST);exit;
			$medicine = D('Medicine');
			$comment = D('MedicineComment');
			$categoryid = $_POST['categoryid'];
			$medicines = $_POST['medicines'];
			
			$data['uid'] = $this->mid;
			$data['categoryid'] = $categoryid;
			foreach($medicines as $id){
				$data['medicineid'] = $id;
				$data['intime'] = time();
				$medicine->setInc('num','id='.$id);
				$comment->add($data);
			}
			$msg = "提交成功！";
			$jump_url = U('Index/show',array('categoryid'=>$categoryid));
			$this->assign('jumpUrl',$jump_url);
			$this->success($msg);
		}

		$this->assign('gcid',$user['group_category_id']);
		$this->assign('categoryList',$categoryList);
		$this->display();
	}
	
	function show(){
		$categoryid = $_GET['categoryid'];
		$comment = D('MedicineComment');
		$cond['ts_medicine_comment.categoryid'] = $categoryid;
		$cond['ts_medicine_comment.uid'] = $this->mid;
		$medicineList = $comment->join('ts_medicine ON ts_medicine_comment.medicineid = ts_medicine.id')->where($cond)->field('ts_medicine_comment.*,ts_medicine.name')->select();
//		dump($medicineList);exit;
		$categoryList = D('GroupCategory')->select();
		
		
		$this->assign('categoryid', $categoryid);
		$this->assign('categoryList', $categoryList);
		$this->assign('medicineList', $medicineList);
		$this->display();
	}
	
	/**
	 * 根据分类列表获取药品
	 */
	function getMedicineByCategoryId(){
		$cond['categoryid'] = $_POST['categoryid'];
		$list = D('Medicine')->where($cond)->select();
		
		
		foreach ($list as $rs){
			$data[$rs['id']] = $rs['name'];
		}
		if($data){
			$this->ajaxReturn($data, '获取数据成功！', 1);
		}else{
			$this->ajaxReturn(null, '获取数据失败！', 0);
		}
	}
	
	/**
	 * 添加药品名称到临时表
	 */
	function addMedicine(){
		$categoryid = $_POST['categoryid'];
		$name = $_POST['name'];
		
		if($this->isAjax()){
			if(!$name){
				$this->ajaxReturn(null,'药物名称不能为空！',0);
			}
	//		dump($categoryid);exit;
			$model = D('MedicineTemp');
			$data['categoryid'] = $categoryid;
			$data['intime'] = time();
			$data['audit'] = 0;
			$data['uid'] = $this->mid;
			$data['name'] = $name;
			
			if(false!==$model->add($data)){
				$this->ajaxReturn(null,'添加成功，请耐心等待管理员审核。',1);
			}else{
				$this->ajaxReturn(null,'数据连接失败，请稍后再试。',0);
			}
		}
		
		$this->display();
	}
	
	/**
	 * 获取最常用药统计数据
	 */
	function getStaticByCategoryid(){
		
		$categoryid = $_POST['categoryid'];
		$model = D('Medicine');
		$cond['categoryid'] = $categoryid;
		
		$list = $model->where($cond)->order('num desc')->select();
		
		if($list){
			$i = 1;
			foreach($list as $rs){
				if($i>5) continue;
				$total += $rs['num'];
				$i++;
			}
			
			$i = 1;
			foreach($list as $key => $rs){
				if($i>5){
					$data['other'][] = array('name'=>$rs['name'], 'num'=>$rs['num']) ;
				}else{
					$data['top5'][] = array('name'=>$rs['name'], 'y'=>round($rs['num']/$total,2), 'num'=>$rs['num']) ;
				}
				$i++;
			}
			
			unset($i);
			$this->ajaxReturn($data, $info='success', 1);
		}
	}
	
	function addComment(){
		if($this->isPost()){
			$model = D('MedicineComment');
			if($data = $model->create()){
//				dump($model->save($data));exit;
				if(false!==$model->save($data)){
					$result = $model->find($data['id']);
					$this->ajaxReturn($result, '提交成功！', 1);
				}else{
					$this->ajaxReturn(null, '提交失败！', 0);
				}
			}else{
				$this->ajaxReturn(null, $model->getError(), 0);
			}
			
		}
	}
	
	function isCommented(){
		$id = $_POST['id'];
		if($this->isPost()){
			$model = D('MedicineComment');
			$data = $model->find($id);
			if($data['content']){
				$this->ajaxReturn($data,'success',1);
			}else{
				$this->ajaxReturn(null,'error',0);
			}
		}
	}
}
?>